package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public int addBed(BedDto bedDto) {
        //TODO  是否需要重名判断
         Integer num= bedMapper.countByBedNum(bedDto.getBedNumber());
         if (num>0){
             throw new BaseException(BasicEnum.BED_INSERT_FAIL_CODE);
         }
        try {
            Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);
            bed.setBedStatus(0);  //床位状态默认0(未入住)
            return bedMapper.inseart(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
     /*   try {
            Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);
            bed.setBedStatus(0);  //床位状态默认0(未入住)
            return bedMapper.inseart(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }*/
    }

    @Override
    public int deleteById(Long id) {
        //TODO  该床位有人则不能删除 ?
        return  bedMapper.deleteById(id);
    }

    @Override
    public BedVo getBed(Long id) {
         Bed bed= bedMapper.getBed(id);
        BedVo bedVo = BeanUtil.copyProperties(bed, BedVo.class);
        return bedVo;
    }

    @Override
    public int updateBed(BedDto bedDto) {
        BedVo bedVo = getBed(bedDto.getId());
        bedVo.setSort(bedDto.getSort());
        bedVo.setBedNumber(bedDto.getBedNumber());
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())) {
            bedVo.setBedStatus(bedDto.getBedStatus());
        }
        Bed bed = BeanUtil.toBean(bedVo, Bed.class);
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        return bedMapper.updateBed(bed);
    }
}

